package com.mls.fivedistrict.server.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mls.fivedistrict.dao.RoleMapper;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Role;
import com.mls.fivedistrict.pojo.RolePermission;
import com.mls.fivedistrict.pojo.RolePermissionDict;
import com.mls.fivedistrict.server.RolePermissionService;
import com.mls.fivedistrict.server.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author Five Team
 * @since 2018-05-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RolePermissionService rolePermissionService;
    @Autowired
    RoleMapper roleMapper;

    @Transactional
    @Override
    public boolean insertAndPermission(Role role) {
        boolean insert = false;
        role.setRolePermissionList(null);
        List<RolePermissionDict> rolePermissionDictList = role.getPermissionDictList();
        role.setPermissionDictList(null);
        insert = insert(role);
        if (!insert) {
            return false;
        }

        List<RolePermission> rolePermissionList = new ArrayList<>();
        if (rolePermissionDictList != null) {
            for (int i = 0; i < rolePermissionDictList.size(); i++) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setRolePermissionDictId(rolePermissionDictList.get(i).getId());
                rolePermissionList.add(rolePermission);
            }
            insert = rolePermissionService.insertBatch(rolePermissionList);
            if (!insert) {
                throw new CustomerException("写入失败");
            }
        }

        return true;
    }

    @Override
    public boolean updateAndPermission(Role role) {
        boolean update = false;
        role.setRolePermissionList(null);
        List<RolePermissionDict> rolePermissionDictList = role.getPermissionDictList();
        role.setPermissionDictList(null);
        update = updateById(role);
        if (!update) {
            return false;
        }
        List<RolePermission> rolePermissionList = new ArrayList<>();
        if (rolePermissionDictList != null) {
            for (int i = 0; i < rolePermissionDictList.size(); i++) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setRolePermissionDictId(rolePermissionDictList.get(i).getId());
                rolePermissionList.add(rolePermission);
            }
            boolean delete = rolePermissionService.delete(Condition.create().eq("role_id", role.getId()));
            if (!delete) {
                throw new CustomerException("写入失败");
            }
            if (rolePermissionDictList.size() == 0) {
                return true;
            }
            boolean insert = rolePermissionService.insertBatch(rolePermissionList);
            if (!delete) {
                throw new CustomerException("写入失败");
            }
        }
        return true;
    }

    @Transactional
    @Override
    public boolean deleteAndPermissions(Long roleId) {
        boolean delete = deleteById(roleId);
        if (!delete) {
            return false;
        }
        delete = rolePermissionService.delete(Condition.create().eq("role_id", roleId));
        if (!delete) {
            throw new CustomerException("删除失败");
        }
        return true;
    }

//    @Override
//    public Role selectAndPermissions(Long roleId) {
//        List<Role> roleList = roleMapper.getRoleAndPermissionListById(roleId);
//        return roleList.get(0);
//    }

    @Override
    public List<Role> selectRolesByUserId(Long userId) {
        List<Role> roleList = roleMapper.selectRolesByUserId(userId);
        return roleList;
    }
}
